package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.handler.BusinessException;
import com.ywgt.response.ResultCode;
import com.ywgt.response.UserStatus;
import com.ywgt.response.UserType;
import com.ywgt.system.pojo.User;
import com.ywgt.system.mapper.UserMapper;
import com.ywgt.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.system.vo.UserVO;
import com.ywgt.utils.IDWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private IDWorker idWorker;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @Override
    public IPage<User> findUserPage(Page<User> page, QueryWrapper<User> queryWrapper) {
        return baseMapper.findUserPage(page,queryWrapper);
    }

    @Override
    public void addUser(User user) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = queryWrapper.eq(User::getUsername, user.getUsername());
        Integer count = baseMapper.selectCount(userLambdaQueryWrapper);
        if (count!=0){
            throw new BusinessException(ResultCode.USER_ACCOUNT_ALREADY_EXIST.getResultCode(),ResultCode.USER_ACCOUNT_ALREADY_EXIST.getResultMessage());
        }
        String password = user.getPassword();
        password= passwordEncoder.encode(password);
        user.setPassword(password);
        user.setId(idWorker.nextId());
        user.setSalt(UUID.randomUUID().toString().substring(0,20).replace("-",""));
        user.setStatus(UserStatus.AVAILABLE.getCode());
        user.setType(UserType.SYSTEM_USER.getCode());
        baseMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId());
        int update = baseMapper.update(user, updateWrapper);
        return update;
    }

    @Override
    public User getUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean checkLogin(String username, String password) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User user = baseMapper.selectOne(queryWrapper);
        boolean matches = passwordEncoder.matches(password, user.getPassword());
        System.out.println(matches);
        return matches;
    }

    @Override
    public Page<User> findUserList(Long pageNum, Long pageSize, UserVO userVO) {
        Page<User> page=new Page<>(pageNum,pageSize);
        Page<User> userPage = userMapper.selectPage(page, getWrapper(userVO));
        return userPage;
    }

    public LambdaQueryWrapper<User> getWrapper(UserVO userVO){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        if(userVO!=null){
            if (!StringUtils.isEmpty(userVO.getDepartmentId())){
                wrapper.eq(User::getDepartmentId,userVO.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVO.getEmail())){
                wrapper.eq(User::getEmail,userVO.getEmail());
            }
            if (!StringUtils.isEmpty(userVO.getNickname())){
                wrapper.eq(User::getNickname,userVO.getNickname());
            }
            if (!StringUtils.isEmpty(userVO.getSex())){
                wrapper.eq(User::getSex,userVO.getSex());
            }
            if (!StringUtils.isEmpty(userVO.getUsername())){
                wrapper.eq(User::getUsername,userVO.getUsername());
            }
        }
        return wrapper;
    }
}
